/**
 * 定时器 hook
 */
export function useTimer() {
  // 存储定时器ID
  let timer: number | null = null;

  // 设置定时器
  const set = (fn: () => void, delay: number) => {
    // 清除之前的定时器
    if (timer) {
      clearTimeout(timer);
    }
    // 设置新的定时器
    timer = setTimeout(fn, delay) as unknown as number;
  };

  // 清除定时器
  const clear = () => {
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
  };

  return {
    set,
    clear
  };
}